/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agol.rent.system.admin;

import agol.rent.system.RunAdmin;
import agol.rent.system.entity.UserApp;
import java.awt.event.ItemEvent;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.SwingWorker;

/**
 *
 * @author AgoL - Daffa
 */
public class dialogLogin extends javax.swing.JDialog {
    
    private boolean notLogin = true;
    private GroupLayout gl;
    private WorkerLogin workerLogin;
    private FormAdmin formAdmin;
    

    /**
     * Creates new form dialogLogin
     */
    
    public dialogLogin() {
        super(RunAdmin.getFormAdmin(), Boolean.TRUE);
        initComponents();
        
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
             public void windowClosing(java.awt.event.WindowEvent e){
                System.out.println(0);
             }
        });
        
        jProgressBar1.setVisible(true);
        
        //gl = (GroupLayout) getContentPane().getLayout();
        //gl.setHonorsVisibility(jProgressBar1, false);
        
        txtuserName.requestFocusInWindow();
        
        setLocationRelativeTo(this);
        jTimeLabel1.startTimer();
        
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTimeLabel1 = new com.stripbandunk.jwidget.JTimeLabel();
        jPanelGradient = new javax.swing.JPanel();
        jLabelUsername = new javax.swing.JLabel();
        jLabelPassword = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        BtnCancel = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxJabatan = new javax.swing.JComboBox();
        txtuserName = new javax.swing.JTextField();
        txtpassword = new javax.swing.JPasswordField();
        label1 = new usu.widget.Label();
        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setUndecorated(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(102, 102, 102));
        jLabel1.setFont(new java.awt.Font("Pristina", 1, 48));
        jLabel1.setForeground(new java.awt.Color(153, 153, 153));
        jLabel1.setText("<html>\n\n<font color=\"red\">Rent</font> \n<font color=\"black\">Shop</font> \n<font color=\"red\">System</font>\n\n</html> ");

        jTimeLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jTimeLabel1.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        jTimeLabel1.setTimeFormatter("dd MMMM yyyy ,  HH:mm:ss");

        jPanelGradient.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 255, 255), new java.awt.Color(236, 233, 216), java.awt.Color.darkGray, java.awt.Color.lightGray));

        jLabelUsername.setFont(new java.awt.Font("Candara", 1, 18));
        jLabelUsername.setForeground(new java.awt.Color(51, 51, 51));
        jLabelUsername.setText("Username   : ");

        jLabelPassword.setFont(new java.awt.Font("Candara", 1, 18));
        jLabelPassword.setForeground(new java.awt.Color(51, 51, 51));
        jLabelPassword.setText("Password    :");

        btnLogin.setFont(new java.awt.Font("Candara", 1, 18));
        btnLogin.setText("Login");
        btnLogin.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        BtnCancel.setFont(new java.awt.Font("Candara", 1, 18));
        BtnCancel.setText("Cancel");
        BtnCancel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        BtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Showcard Gothic", 0, 18));
        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setText(" Type          :");

        jComboBoxJabatan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "Administrator", "Operator", "Guest" }));
        jComboBoxJabatan.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxJabatanItemStateChanged(evt);
            }
        });
        jComboBoxJabatan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxJabatanActionPerformed(evt);
            }
        });

        label1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        label1.setIconReflec(new javax.swing.ImageIcon(getClass().getResource("/agol/rent/system/image/Login Icon.png"))); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setForeground(new java.awt.Color(0, 0, 153));
        jLabel5.setText("@Agolsantoso");

        javax.swing.GroupLayout jPanelGradientLayout = new javax.swing.GroupLayout(jPanelGradient);
        jPanelGradient.setLayout(jPanelGradientLayout);
        jPanelGradientLayout.setHorizontalGroup(
            jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelGradientLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelGradientLayout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelGradientLayout.createSequentialGroup()
                                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(10, 10, 10))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelGradientLayout.createSequentialGroup()
                                .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabelUsername)
                                        .addComponent(jLabelPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jComboBoxJabatan, 0, 174, Short.MAX_VALUE)
                                    .addComponent(txtpassword)
                                    .addComponent(txtuserName)))))
                    .addGroup(jPanelGradientLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel5)
                        .addGap(89, 89, 89)
                        .addComponent(btnLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BtnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanelGradientLayout.setVerticalGroup(
            jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelGradientLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelGradientLayout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5))
                    .addGroup(jPanelGradientLayout.createSequentialGroup()
                        .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelUsername)
                            .addComponent(txtuserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelPassword)
                            .addComponent(txtpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jComboBoxJabatan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(19, 19, 19)
                        .addGroup(jPanelGradientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnLogin)
                            .addComponent(BtnCancel))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 2, 18));
        jLabel3.setText("Rental Management System");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(172, Short.MAX_VALUE)
                        .addComponent(jPanelGradient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(168, 168, 168)
                        .addComponent(jLabel3)
                        .addGap(0, 235, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addComponent(jTimeLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTimeLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 19, Short.MAX_VALUE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelGradient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public boolean showDialog(){
      setVisible(true);
      return notLogin;
    }

    public void closeLoginDialog(){
       this.dispose();
    }
    
    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        // TODO add your handling code here:

        if (workerLogin != null && workerLogin.isDone()){
            workerLogin.cancel(true);
            workerLogin = null;
        }
        
        workerLogin = new WorkerLogin();
        workerLogin.execute();

    }//GEN-LAST:event_btnLoginActionPerformed

    private void BtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_BtnCancelActionPerformed

    private void jComboBoxJabatanItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxJabatanItemStateChanged
        // TODO add your handling code here:

        if (evt.getStateChange() == ItemEvent.SELECTED) {
            String jabatan = (String) jComboBoxJabatan.getSelectedItem();
            System.out.println(jabatan);

        }
    }//GEN-LAST:event_jComboBoxJabatanItemStateChanged

    private void jComboBoxJabatanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxJabatanActionPerformed
        // TODO add your handling code here:


  }//GEN-LAST:event_jComboBoxJabatanActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        
        System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    
    private class WorkerLogin extends SwingWorker {

        @Override
       protected void done() {
           try{
              if (get() != null ){
                 notLogin = false;
                 closeLoginDialog();
              } else {
                 gl.replace(jProgressBar1, rootPane);
                 txtuserName.requestFocusInWindow();
                 txtpassword.selectAll();
              }
                 btnLogin.setEnabled(true);
                 jProgressBar1.setIndeterminate(false);
                 jProgressBar1.setVisible(true);
           } catch (InterruptedException ex){
                Logger.getLogger(dialogLogin.class.getName()).log(Level.SEVERE, null, ex);
           } catch (ExecutionException ex){
           
           }
       }

        @Override
        protected UserApp doInBackground() throws Exception { //implementasi jProgressbar
           Thread.sleep(1000);
           return RunAdmin.getLoginService().login(txtuserName.getText(),
                 String.valueOf(txtpassword.getPassword()));
        }
    }
    /**
     * @param args the command line arguments
     */
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnCancel;
    private javax.swing.JButton btnLogin;
    private javax.swing.JComboBox jComboBoxJabatan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelPassword;
    private javax.swing.JLabel jLabelUsername;
    private javax.swing.JPanel jPanelGradient;
    private javax.swing.JProgressBar jProgressBar1;
    private com.stripbandunk.jwidget.JTimeLabel jTimeLabel1;
    private usu.widget.Label label1;
    private javax.swing.JPasswordField txtpassword;
    private javax.swing.JTextField txtuserName;
    // End of variables declaration//GEN-END:variables
}
